package com.jeck.action.cu_loginadmin.data;

import com.jeck.conexion.Conector;
import com.jeck.tools.Menu;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author bicho
 */
public class mg_Login {

    static Conector cn = new Conector();

    public static int getIdUser(String user, String pass) {
        ResultSet rs = cn.Consultar("SELECT * FROM check_password_getID('"+user+"','"+pass+"');");//"SELECT id_usuario FROM usuario U WHERE U.nombre ='"+user+"' AND U.pass ='"+pass+"';");
        int id = 0;
        try {
            while (rs.next()) {
                System.out.println("Se logueo el usuario: "+user);
                id = rs.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(mg_Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }

    public static List getMenuByIdUser(int id) {
        System.out.println("accediendo getMenuByIdUser");

        List menu = new ArrayList();
        ResultSet rs = cn.Consultar("SELECT M.nombre FROM usuario U inner join " +
                "rol R on U.id_rol = R.id_rol inner join menu_rol N on " +
                "R.id_rol = N.id_rol inner join menu M on N.id_menu = M.id_menu " +
                "WHERE id_usuario = 1 ;");
        try {
            while (rs.next()) {
                System.out.println("añadido :"+rs.getString(1));
                Menu m = new Menu(rs.getString("nombre"), rs.getString("nombre").replace(" ", "")+".action");
                menu.add(m);
            }
        } catch (SQLException ex) {
            Logger.getLogger(mg_Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        return menu;
    }
}
